package threadingexercise;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//使用阻塞队列
public class ThreadDemo19BlockingQueue {
    public static void main(String[] args) throws InterruptedException {

        // BlockingQueue 是一个接口，不能直接 new 实例，可以new 它的实现类：LinkedBlockingQueue 和 ArrayBlockingQueue
        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue();
        //put  入队列
        blockingQueue.put("hello");
        blockingQueue.put("cat");
        blockingQueue.put("dog");
        blockingQueue.put("abandon");



        //take 出队列
        String ret = blockingQueue.take();
        System.out.println(ret);

        String ret2 = blockingQueue.take();
        System.out.println(ret2);

        String ret3 = blockingQueue.take();
        System.out.println(ret3);

        String ret4 = blockingQueue.take();
        System.out.println(ret4);

        //第五次 队列里没有元素了 阻塞等待
        String ret5 = blockingQueue.take();
        System.out.println(ret5);

    }
}
